package com.relytosoft.mizar.media.analyze.services.service.structure.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.exception.ServiceException;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.redis.utils.RedisUtil;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.analyze.api.entity.structure.AyStructureDisposition;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyPlanTemplate;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyStruControlVehicleDto;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyStructureDispositionDto;
import com.relytosoft.mizar.media.analyze.api.pojo.query.AyStructureDispositionQuery;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyBatchVo;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyStrucDispositionVo;
import com.relytosoft.mizar.media.analyze.services.mapper.structure.AyStructureDispositionMapper;
import com.relytosoft.mizar.media.analyze.services.service.structure.AyStructureDispositionService;
import com.relytosoft.mizar.media.analyze.services.service.structure.AyStructureTaskSonService;
import com.relytosoft.mizar.media.analyze.services.service.system.AyPlanTemplateService;
import com.relytosoft.mizar.media.common.constant.AlarmTypeConstants;
import com.relytosoft.mizar.media.common.constant.AyCacheConstants;
import com.relytosoft.mizar.media.common.constant.MediaMqConstants;
import com.relytosoft.mizar.media.common.emqx.client.MqttOp;
import com.relytosoft.mizar.media.common.entity.pojo.dto.AyDispositionPlanDto;
import com.relytosoft.mizar.media.common.entity.pojo.dto.StruDispositionReqDto;
import com.relytosoft.mizar.media.common.enums.ControlStatusEnum;
import com.relytosoft.mizar.media.common.enums.DeleteStatusEnum;
import com.relytosoft.mizar.media.common.enums.StruDispositionTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * AyStructureDispositionServiceImpl
 *
 * @Author SunLi
 * @Date: 2022/6/2 16:34
 * @Version 1.0
 */
@Service
public class AyStructureDispositionServiceImpl extends ServiceImpl<AyStructureDispositionMapper, AyStructureDisposition> implements AyStructureDispositionService {
    @Resource
    private AyStructureTaskSonService taskSonService;

    @Resource
    private AyPlanTemplateService ayPlanTemplateService;

    @Override
    public IPage<AyStrucDispositionVo> selectStruDispositionPageList(PageQuery pageQuery, AyStructureDispositionQuery dispositionQuery) {
        if (StrUtil.isBlank(pageQuery.getOrderByColumn())) {
            pageQuery.setOrderByColumn("id");
            pageQuery.setIsAsc("desc");
        }
        if(StrUtil.isNotEmpty(dispositionQuery.getEndTime())){
            LocalDateTime endTime= LocalDateTimeUtil.parse(dispositionQuery.getEndTime());
            dispositionQuery.setEndTime(endTime.plusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE));
        }
        return baseMapper.selectPageDispositionList(pageQuery.build(),dispositionQuery);
    }

    @Override
    public AyBatchVo insertAyStructureDisposition(AyStructureDispositionDto dispositionDto) {
        AyStructureDisposition structureDisposition=buildDisposition(dispositionDto);
        //获取需要添加布控任务得设备ID
        String[] deviceIds = structureDisposition.getDispositionRangeDevice().split(",");
        AyBatchVo ayBatchVo = new AyBatchVo();
        ayBatchVo.setTotal(deviceIds.length);

        //筛选出有运行中的结构化分析任务的设备，再进行添加
        String[] verifyIds=taskSonService.getRunTaskDeviceIds(deviceIds);
        if(verifyIds==null||verifyIds.length==0){
            ayBatchVo.setSuccess(0);
            ayBatchVo.setError(ayBatchVo.getTotal());
            ayBatchVo.setErrorMsg(Collections.singletonList("所选设备没有运行中的实时分析任务"));
            return ayBatchVo;
        }

        //更新 布控任务设备ID集合
        structureDisposition.setDispositionRangeDevice(ArrayUtil.join(verifyIds,","));
        if(this.save(structureDisposition)){
            ayBatchVo.setSuccess(verifyIds.length);
            ayBatchVo.setError(ayBatchVo.getTotal()-ayBatchVo.getSuccess());
            ayBatchVo.setErrorMsg(Collections.singletonList("布控任务创建成功"));
        }
        return ayBatchVo;
    }

    @Override
    public AyStructureDisposition getAyStructureDispositionById(Integer id) {
        return this.getById(id);
    }

    @Override
    public AyBatchVo updateAyStructureDisposition(AyStructureDispositionDto dispositionDto) {

        AyStructureDisposition structureDisposition=buildDisposition(dispositionDto);

        //获取需要添加布控任务的设备ID
        String[] deviceIds = structureDisposition.getDispositionRangeDevice().split(",");
        AyBatchVo ayBatchVo = new AyBatchVo();
        ayBatchVo.setTotal(deviceIds.length);
        //筛选出有运行中的结构化分析任务的设备，再进行添加
        String[] verifyIds=taskSonService.getRunTaskDeviceIds(deviceIds);
        if(verifyIds==null||verifyIds.length==0){
            ayBatchVo.setSuccess(0);
            ayBatchVo.setError(ayBatchVo.getTotal());
            ayBatchVo.setErrorMsg(Collections.singletonList("所选设备没有运行中的实时分析任务"));
            return ayBatchVo;
        }
        //更新 布控任务设备ID集合
        structureDisposition.setDispositionRangeDevice(ArrayUtil.join(verifyIds,","));

        if(this.updateById(structureDisposition)){
            ayBatchVo.setSuccess(verifyIds.length);
            ayBatchVo.setError(ayBatchVo.getTotal()-ayBatchVo.getSuccess());
            ayBatchVo.setErrorMsg(Collections.singletonList("布控任务修改成功"));
        }
        return ayBatchVo;

    }

    @Override
    public AyBatchVo deleteAyStructureDisposition(List<Integer> dispositionIds) {
        AyBatchVo ayBatchVo = new AyBatchVo();
        ayBatchVo.setTotal(dispositionIds.size());
        //查询可删除的任务[运行中或布防中不允许删除]
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.ne(AyStructureDisposition::getStatus,ControlStatusEnum.RUNING.getValue());
        wrapper.eq(AyStructureDisposition::getDefenseStatus,0);
        wrapper.eq(AyStructureDisposition::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue());
        wrapper.in(AyStructureDisposition::getId, dispositionIds);
        wrapper.select(AyStructureDisposition::getId);
        List<Object> infos = this.listObjs(wrapper);
        Integer[] verifyIds =infos.toArray(new Integer[]{});
        if(verifyIds==null||verifyIds.length==0){
            ayBatchVo.setSuccess(0);
            ayBatchVo.setError(ayBatchVo.getTotal());
            ayBatchVo.setErrorMsg(Collections.singletonList("没有符合条件的布控任务"));
            return ayBatchVo;
        }
        //删除任务
        int rowCount=deleteDisposition(Arrays.asList(verifyIds));
        ayBatchVo.setSuccess(rowCount);
        ayBatchVo.setError(ayBatchVo.getTotal()-ayBatchVo.getSuccess());
        ayBatchVo.setErrorMsg(Collections.singletonList("符合条件的布控任务已删除"));
        return ayBatchVo;
    }

    @Override
    public AyBatchVo startStructureDisposition(List<Integer> dispositionIds) {
        AyBatchVo ayBatchVo = new AyBatchVo();
        ayBatchVo.setTotal(dispositionIds.size());

        //查询可启动的任务[未启动、已停止、异常]
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.ne(AyStructureDisposition::getStatus,ControlStatusEnum.RUNING.getValue());
        wrapper.eq(AyStructureDisposition::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue());
        wrapper.in(AyStructureDisposition::getId, dispositionIds);
        wrapper.select(AyStructureDisposition::getId);
        List<Object> infos = this.listObjs(wrapper);
        Integer[] verifyIds =infos.toArray(new Integer[]{});
        if(verifyIds==null||verifyIds.length==0){ ayBatchVo.setSuccess(0);
            ayBatchVo.setError(ayBatchVo.getTotal());
            ayBatchVo.setErrorMsg(Collections.singletonList("没有符合条件的布控任务"));
            return ayBatchVo;
        }
        try {
            List<Integer> ids=Arrays.asList(verifyIds);
            //更新任务状态为布控中、布防中
            startDisposition(ids);
            ayBatchVo.setSuccess(verifyIds.length);
            ayBatchVo.setError(ayBatchVo.getTotal()-ayBatchVo.getSuccess());
            ayBatchVo.setErrorMsg(Collections.singletonList("符合条件的布控任务已启动"));
            //发送消息同步任务至比特大陆
            sendCreateMsg(ids);

        }
        catch (Exception ex){
            ayBatchVo.setSuccess(0);
            ayBatchVo.setError(ayBatchVo.getTotal());
            ayBatchVo.setErrorMsg(Collections.singletonList("没有符合条件的布控任务"));
        }
        return ayBatchVo;
    }

    @Override
    public AyBatchVo stopStructureDisposition(List<Integer> dispositionIds) {
        AyBatchVo ayBatchVo = new AyBatchVo();
        ayBatchVo.setTotal(dispositionIds.size());

        //查询可停止的任务[运行中]
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AyStructureDisposition::getStatus,ControlStatusEnum.RUNING.getValue());
        wrapper.eq(AyStructureDisposition::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue());
        wrapper.in(AyStructureDisposition::getId, dispositionIds);
        wrapper.select(AyStructureDisposition::getId);
        List<Object> infos = this.listObjs(wrapper);
        Integer[] verifyIds =infos.toArray(new Integer[]{});
        if(verifyIds==null||verifyIds.length==0){ ayBatchVo.setSuccess(0);
            ayBatchVo.setError(ayBatchVo.getTotal());
            ayBatchVo.setErrorMsg(Collections.singletonList("没有符合条件的布控任务"));
            return ayBatchVo;
        }
        try {
            List<Integer> ids=Arrays.asList(verifyIds);
            //更新任务状态为已停止、撤防
            stopDisposition(ids);
            ayBatchVo.setSuccess(verifyIds.length);
            ayBatchVo.setError(ayBatchVo.getTotal()-ayBatchVo.getSuccess());
            ayBatchVo.setErrorMsg(Collections.singletonList("符合条件的布控任务已停止"));
            //发送消息同步任务至比特大陆
            sendDeleteMsg(ids);
        }
        catch (Exception ex){
            ayBatchVo.setSuccess(0);
            ayBatchVo.setError(ayBatchVo.getTotal());
            ayBatchVo.setErrorMsg(Collections.singletonList("没有符合条件的布控任务"));
        }
        return ayBatchVo;
    }

    @Override
    public boolean armedAndDisarmed(List<Integer> dispositionIds) {
        List<AyStructureDisposition> structureDisposition = this.listByIds(dispositionIds);
        structureDisposition.forEach(item -> {
            item.setDefenseStatus(item.getDefenseStatus() == 0 ? 1 : 0);
            if(item.getDispositionType().equals(StruDispositionTypeEnum.VEHICLEGROUP.getValue())){
                //车辆库布控按更新redis布防撤防信息
                createRedis(item);
            }
            else{
                if(!Objects.isNull(item.getDispositionId())){
                    //更新redis布防撤防信息
                    RedisUtil.set(AyCacheConstants.CONTROL_STRUCTURE_CONDITION + item.getDispositionId(), JSON.toJSONString(item));
                }
            }
        });
        return this.updateBatchById(structureDisposition);
    }

    @Override
    public boolean updateTempVehicleGroup(AyStruControlVehicleDto controlVehicleDto) {
        AyStructureDisposition disposition=new AyStructureDisposition();
        disposition.setId(controlVehicleDto.getId());
        disposition.setTempVehicleGroup(controlVehicleDto.getTempVehicleGroup());
        if(StrUtil.isNotEmpty(controlVehicleDto.getTempBeginTime())){
            disposition.setTempBeginTime(Convert.toLocalDateTime(controlVehicleDto.getTempBeginTime()));
        }
        else{
            disposition.setTempBeginTime(null);
        }
        if(StrUtil.isNotEmpty(controlVehicleDto.getTempEndTime())){
            disposition.setTempEndTime(Convert.toLocalDateTime(controlVehicleDto.getTempEndTime()));
        }
        else{
            disposition.setTempEndTime(null);
        }
        return baseMapper.update(null,new LambdaUpdateWrapper<AyStructureDisposition>().set(AyStructureDisposition::getTempVehicleGroup,disposition.getTempVehicleGroup()).set(AyStructureDisposition::getUpdatedId, CurrentUser.getCurrentUserId()).set(AyStructureDisposition::getUpdatedBy, CurrentUser.getCurrentUserFullName()).set(AyStructureDisposition::getUpdatedTime, LocalDateTime.now()).set(AyStructureDisposition::getTempBeginTime,disposition.getTempBeginTime()).set(AyStructureDisposition::getTempEndTime,disposition.getTempEndTime())
                .eq(AyStructureDisposition::getId,disposition.getId()).eq(AyStructureDisposition::getIsDeleted,DeleteStatusEnum.NOT_DELETED.getValue()))==1;
    }
    private AyStructureDisposition buildDisposition(AyStructureDispositionDto dispositionDto){
        AyStructureDisposition disposition= BeanCopyUtils.copy(dispositionDto,AyStructureDisposition.class);
        if(dispositionDto.getId()!=null){
            //修改时验证数据
            //查询原来数据的状态信息
            AyStructureDisposition dispositionCurrent = this.getById(dispositionDto.getId());
            if(dispositionCurrent==null){
                throw new ServiceException("布控任务不存在");
            }
            //任务状态为运行中不允许修改
            if(dispositionCurrent.getStatus()==ControlStatusEnum.RUNING.getValue()){
                throw new ServiceException("布控任务不允许修改");
            }
        }
        else if(dispositionDto.getDispositionType().equals(StruDispositionTypeEnum.VEHICLEGROUP.getValue())){
            //车辆库布控时，随机生成布控任务GUID
            disposition.setDispositionId(UUID.randomUUID().toString());
        }
        //获取计划模板信息
        AyPlanTemplate planTemplate = null;
        if (dispositionDto.getPlanTemplateId() != null) {
            planTemplate = ayPlanTemplateService.getById(dispositionDto.getPlanTemplateId());
            disposition.setPlanInfoNum(planTemplate.getPlanInfoNum());
            disposition.setPlanType(planTemplate.getPlanType());
            disposition.setPlanInfoList(planTemplate.getPlanInfoList());
        }
        //设置任务初始状态为未启动
        disposition.setStatus(ControlStatusEnum.NOT_STARTED.getValue());
        //任务状态与布防状态联动 撤防
        disposition.setDefenseStatus(0);
        return disposition;
    }

    private int deleteDisposition(List<Integer> ids) {
        //运行中或布防中的任务不允许删除
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.ne(AyStructureDisposition::getStatus,ControlStatusEnum.RUNING.getValue());
        wrapper.eq(AyStructureDisposition::getDefenseStatus,0);
        wrapper.eq(AyStructureDisposition::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue());
        wrapper.in(AyStructureDisposition::getId, ids);

        AyStructureDisposition structureDisposition = new AyStructureDisposition();
        structureDisposition.setIsDeleted(DeleteStatusEnum.DELETED.getValue());
        structureDisposition.setUpdatedId(CurrentUser.getCurrentUserId());
        structureDisposition.setUpdatedBy(CurrentUser.getCurrentUserFullName());
        return baseMapper.update(structureDisposition, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public void startDisposition(List<Integer> ids){
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.ne(AyStructureDisposition::getStatus,ControlStatusEnum.RUNING.getValue());
        wrapper.eq(AyStructureDisposition::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue());
        wrapper.in(AyStructureDisposition::getId, ids);

        AyStructureDisposition structureDisposition = new AyStructureDisposition();
        structureDisposition.setStatus(ControlStatusEnum.RUNING.getValue());
        structureDisposition.setDefenseStatus(1);//布防中
        structureDisposition.setUpdatedId(CurrentUser.getCurrentUserId());
        structureDisposition.setUpdatedBy(CurrentUser.getCurrentUserFullName());
        structureDisposition.setBeginTime(LocalDateTime.now());
        structureDisposition.setEndTime(null);
        int rowCount=baseMapper.update(structureDisposition, wrapper);
        if(rowCount!=ids.size()){
            throw new ServiceException("任务状态发生变化，请查询后重试");
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public void stopDisposition(List<Integer> ids){
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AyStructureDisposition::getStatus,ControlStatusEnum.RUNING.getValue());
        wrapper.eq(AyStructureDisposition::getIsDeleted, DeleteStatusEnum.NOT_DELETED.getValue());
        wrapper.in(AyStructureDisposition::getId, ids);

        AyStructureDisposition structureDisposition = new AyStructureDisposition();
        structureDisposition.setStatus(ControlStatusEnum.FINISHED.getValue());
        structureDisposition.setDefenseStatus(0);//撤防
        structureDisposition.setUpdatedId(CurrentUser.getCurrentUserId());
        structureDisposition.setUpdatedBy(CurrentUser.getCurrentUserFullName());
        structureDisposition.setEndTime(LocalDateTime.now());
        int rowCount=baseMapper.update(structureDisposition, wrapper);
        if(rowCount!=ids.size()){
            throw new ServiceException("任务状态发生变化，请查询后重试");
        }
    }

    @Override
    public void createRedis(AyStructureDisposition disposition){
        if(!disposition.getDispositionType().equals(StruDispositionTypeEnum.VEHICLEGROUP.getValue())){
            return;
        }
        //车辆库布控按设备添加redis布控任务
        String[] deviceIds=disposition.getDispositionRangeDevice().split(",");
        for (String deviceId:deviceIds){
            AyDispositionPlanDto planDto=BeanCopyUtils.copy(disposition,AyDispositionPlanDto.class);
            planDto.setDeviceId(deviceId);
            if(StrUtil.isNotEmpty(disposition.getTempVehicleGroup())){
                planDto.setBeginTime(disposition.getTempBeginTime());
                planDto.setEndTime(disposition.getTempEndTime());
            }else{
                planDto.setBeginTime(null);
                planDto.setEndTime(null);
            }
            planDto.setAlarmTypeId(AlarmTypeConstants.VEHICLE_ALARM_CONTROL);
            planDto.setTaskName(disposition.getName());

            String redisKey=AyCacheConstants.RAW_STRUCTURE_CONDITION + deviceId;
            Object controlJson =RedisUtil.get(redisKey);
            List<AyDispositionPlanDto> dispositionPlanDtos=new ArrayList<>();
            if(!Objects.isNull(controlJson)){
                dispositionPlanDtos=JSON.parseArray(controlJson.toString(), AyDispositionPlanDto.class);
            }
            AyDispositionPlanDto controlOld=dispositionPlanDtos.stream().filter(d->d.getDispositionId().equals(disposition.getDispositionId())).findAny().orElse(null);
            if(Objects.isNull(controlOld)){
                dispositionPlanDtos.add(planDto);
            }else{
                dispositionPlanDtos.remove(controlOld);
                dispositionPlanDtos.add(planDto);
            }
            RedisUtil.set(redisKey, JSON.toJSONString(dispositionPlanDtos));
        }
    }
    @Override
    public void deleteRedis(AyStructureDisposition disposition) {
        if(!disposition.getDispositionType().equals(StruDispositionTypeEnum.VEHICLEGROUP.getValue())){
            return;
        }
        //车辆库布控按设备添加redis布控任务
        String[] deviceIds = disposition.getDispositionRangeDevice().split(",");
        for (String deviceId : deviceIds) {
            String redisKey=AyCacheConstants.RAW_STRUCTURE_CONDITION + deviceId;
            Object controlJson =RedisUtil.get(redisKey);
            List<AyDispositionPlanDto> dispositionPlanDtos=new ArrayList<>();
            if(!Objects.isNull(controlJson)){
                dispositionPlanDtos=JSON.parseArray(controlJson.toString(), AyDispositionPlanDto.class);
            }
            AyDispositionPlanDto controlOld=dispositionPlanDtos.stream().filter(d->d.getDispositionId().equals(disposition.getDispositionId())).findAny().orElse(null);
            if (!Objects.isNull(controlOld)) {
                dispositionPlanDtos.remove(controlOld);
            }
            if(dispositionPlanDtos.isEmpty()){
                RedisUtil.del(redisKey);
            }
            else{
                RedisUtil.set(redisKey, JSON.toJSONString(dispositionPlanDtos));
            }
        }
    }

    public void sendCreateMsg(List<Integer> ids){
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyStructureDisposition::getId, ids);
        wrapper.eq(AyStructureDisposition::getStatus,ControlStatusEnum.RUNING.getValue());
        List<AyStructureDisposition> dispositionList=this.list(wrapper);
        if(ArrayUtil.isNotEmpty(dispositionList)){
            for (AyStructureDisposition disposition:dispositionList){
                if(disposition.getDispositionType().equals(StruDispositionTypeEnum.VEHICLEGROUP.getValue())) {
                    //车辆库布控直接记录信息至redis
                    createRedis(disposition);
                }
                else{
                    //文本及图片布控 需发送消息 同步至比特大陆（同步成功后再更新redis）
                    StruDispositionReqDto dispositionReqDto = createDispositionReqDto(disposition);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("OpType", "insertDisposition");
                    jsonObject.put("Data", JSONObject.toJSONString(dispositionReqDto));
                    //发送消息
                    MqttOp.publish(MediaMqConstants.STRUCTURE_DISPOSITION_TOPIC, jsonObject.toJSONString());
                }
            }
        }
    }


    public void sendDeleteMsg(List<Integer> ids) {
        LambdaQueryWrapper<AyStructureDisposition> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AyStructureDisposition::getId, ids);
        wrapper.eq(AyStructureDisposition::getStatus,ControlStatusEnum.FINISHED.getValue());
        wrapper.isNotNull(AyStructureDisposition::getDispositionId);

        List<AyStructureDisposition> dispositionList=this.list(wrapper);
        if(ArrayUtil.isNotEmpty(dispositionList)){
            for (AyStructureDisposition disposition:dispositionList){
                if(disposition.getDispositionType().equals(StruDispositionTypeEnum.VEHICLEGROUP.getValue())){
                    //删除布控任务
                    deleteRedis(disposition);
                }
                else{
                    //删除redis布控任务
                    RedisUtil.del(AyCacheConstants.CONTROL_STRUCTURE_CONDITION + disposition.getDispositionId());
                    //文本及图片布控 需发送消息 同步至比特大陆
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("OpType", "deleteDisposition");
                    jsonObject.put("Data", disposition.getDispositionId());
                    //发送消息
                    MqttOp.publish(MediaMqConstants.STRUCTURE_DISPOSITION_TOPIC, jsonObject.toJSONString());
                }
            }
        }
    }

    public StruDispositionReqDto createDispositionReqDto(AyStructureDisposition ayStructureDisposition){
        StruDispositionReqDto dispositionReqDto=new StruDispositionReqDto();
        dispositionReqDto.setId(ayStructureDisposition.getId());
        dispositionReqDto.setDeviceId(ayStructureDisposition.getDispositionRangeDevice());
        dispositionReqDto.setType(ayStructureDisposition.getDispositionType());
        //文本布控
        if(ayStructureDisposition.getDispositionType().equals(StruDispositionTypeEnum.TEXT.getValue())){
            dispositionReqDto.setTextAttribute(ayStructureDisposition.getDispositionText());
        }
        else{
            dispositionReqDto.setThreshold(Double.valueOf(ayStructureDisposition.getThreshold()));
            dispositionReqDto.setFeatureData(ayStructureDisposition.getDispositionImage());
        }
        return dispositionReqDto;
    }
}
